import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box

import androidx.compose.foundation.layout.Column

import androidx.compose.foundation.layout.fillMaxHeight

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonColors
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.TextUnitType
import androidx.compose.ui.unit.dp
import com.example.senior_homework.entity.Senior

/*
*author:guyueyuan
*createTime:2024/11/18
*/

//这次作业中使用了大量的方法回调，也更加增加了我对jetpack compose的理解
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DataItem(
    data: Senior,
    removeClicked:(Int)->Unit,
    checkDetails:()->Unit,
) {

    var showDeleteDialog by remember { mutableStateOf(false) }

    Box(
        modifier = Modifier
            .height(100.dp)
            .fillMaxWidth(0.95f)
            .clip(RoundedCornerShape(16.dp))
            .background(color = Color.White)
            .padding(10.dp)
    ){
        Column(
            verticalArrangement = Arrangement.spacedBy(8.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
            modifier = Modifier
                .fillMaxWidth()
                .fillMaxHeight()
                .clickable {
                    checkDetails()
                }
        ) {
            Text(
                text = data.title,
                fontSize = TextUnit(16f, TextUnitType.Sp),
                color = Color.Black,
                fontWeight = FontWeight.Bold

            )
            Text(
                style = TextStyle(
                    color = Color(0xff828282)
                ),
                text = data.content,
                maxLines = 2,
                overflow = TextOverflow.Ellipsis,
                textAlign = TextAlign.Start
            )
        }

        Button(
            onClick = {
//                这里蕴含着重大的重组认识的问题
//                总结在于，重组是主线程，但是现在变量改变与判断的也是在主线程，重组在这之后
//                见到那就是，这个onclick执行完了才会重组
//                Log.i("czdwt","bt1")
//                ifConfirmDelete=false
                showDeleteDialog=true
//                Log.i("czdwt","bt2")
//                if (ifConfirmDelete){
//                    removeClicked(data.id)
//                }
            },
            colors = ButtonColors(Color(0x88FC0A30),Color.White,Color.Gray,Color.Black),
            modifier = Modifier.align(Alignment.CenterEnd)
        ) {
            Text("移除")
        }

        if(showDeleteDialog==true){
            Log.i("czdwt","dl1")
            AlertDialog(
                modifier = Modifier.height(200.dp).width(240.dp),
                onDismissRequest = {showDeleteDialog=false},
                title = { Text("提示") },
                text = { Text("确认删除？") },
                confirmButton = {
                    TextButton(
                        onClick = {
//                            Log.i("czdwt","dl2")
//                            ifConfirmDelete=true
//                            showDeleteDialog=false
//                            Log.i("czdwt","dl3")
                            removeClicked(data.id)
                            showDeleteDialog=false
                        }
                    ) {
                        Text("确认")
                    }
                },
                dismissButton = {
                    TextButton(
                        onClick = {
                            showDeleteDialog=false
                        }
                    ) {
                        Text("取消")
                    }
                }
            )
        }
    }

}

@Preview
@Composable
fun DataItemPre() {

}

/*
*
2024-11-19 11:58:49.617 20266-20266 czdwt                   com.example.senior_homework          I  bt1
2024-11-19 11:58:49.617 20266-20266 czdwt                   com.example.senior_homework          I  bt2
2024-11-19 11:58:49.628 20266-20266 czdwt                   com.example.senior_homework          I  dl1
2024-11-19 11:58:54.854 20266-20266 czdwt                   com.example.senior_homework          I  dl2
2024-11-19 11:58:54.855 20266-20266 czdwt                   com.example.senior_homework          I  dl3
*
* */